<template>
  <div id='create-or-edit'>
    <el-card>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="spaceKey">
          <el-input v-model="form.spaceKey"></el-input>
        </el-form-item>
        <el-form-item>
          <div style="text-align: right">
            <el-button @click="handleSubmit">提交</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { saveOrUpdateSpace, getSpaceById } from '@/services/advert-space'

export default {
  props: {
    id: {
      type: [Number, String],
      require: true
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        name: '',
        isDel: 0,
        spaceKey: ''
      }
    }
  },
  methods: {
    async handleSubmit () {
      const { data } = await saveOrUpdateSpace(this.form)
      console.log(data)
      if (data.state === 1) {
        this.$message.success('操作成功')
        this.$router.push('/advertSpace')
      }
    }
  },
  async created () {
    console.log(this.id, this.isEdit)
    if (this.isEdit) {
      const { data } = await getSpaceById(this.id)
      if (data.state === 1) {
        this.form = data.content
      }
    }
  }
}

</script>
<style scoped>

</style>
